package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.GoodsTypeDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SaleListGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Autowired
    private GoodsTypeDao goodsTypeDao;

    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for(int i = 4;i > intCode.toString().length();i--){

            unitCode = "0"+unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }

    @Override
    public Map<String, Object> listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        if (page==1){
            page=0;
        }
       List<Goods> list = goodsDao.listInventory(page,rows,codeOrName,goodsTypeId);
        int size = list.size();
        Map<String, Object> map = new HashMap<>();
        map.put("total", size);
        map.put("rows", list);
        return map;


    }
    //分页查询无库存商品信息
    @Override
    public Map<String, Object> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        if (page==1){
            page=0;
        }
       List<Goods> goodsList = goodsDao.getNoInventoryQuantity(page,rows,nameOrCode);
        Map<String, Object> map = new HashMap<>();
        int size = goodsList.size();
        map.put("total", size);
        map.put("rows", goodsList);
        return map;
    }

    //分页查询有库存商品信息
    @Override
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        if (page==1){
            page=0;
        }
      List<Goods> goodsList =  goodsDao.getHasInventoryQuantity(page, rows, nameOrCode);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", goodsList.size());
        map.put("rows", goodsList);
        return map;
    }
    //添加商品期初库存
    @Override
    public void saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        goodsDao.saveStock(goodsId, inventoryQuantity, purchasingPrice);
    }

    //删除商品库存
    @Override
    public void deleteStock(Integer goodsId) {
        Goods goods = goodsDao.getGoods(goodsId);
        Integer state = goods.getState();
        if (state==0){
            goodsDao.deleteStock(goodsId);
        }

    }


}
